package com.jt.provider.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalTime;


/**
 * 定义一个基于时间的控制拦截器
 * */
public class TimeInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取当前系统时间
        LocalTime nowTime = LocalTime.now();
        int hour = nowTime.getHour();
        if(hour<9 || hour>17){
            //false表示请求到此结果，true表示放行会去执行后面的拦截器或controller对象
            throw new RuntimeException("请在9点--17点之间进行访问！！");//return false;
        }
        return true;
    }
}
